/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentacion;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import logica.ControladorCaracteristicas;

/**
 *
 * @author Andres
 */
public abstract class AZPanel extends javax.swing.JPanel {

    protected ControladorCaracteristicas caracteristicas;
    
    Comparator comparador_numerico = new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            int io1 = ((o1.equals("")) ? 0 : Integer.parseInt(o1));
            int io2 = ((o2.equals("")) ? 0 : Integer.parseInt(o2));
            return io1 - io2;
        }
    };
    
    Comparator comparador_talles = new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            List<String> l = caracteristicas.getCaracteristica("talles");
            if (o1.equals("*")) {
                return Integer.MAX_VALUE;
            }
            if (o2.equals("*")) {
                return Integer.MIN_VALUE;
            }
            int io1 = l.indexOf(o1);
            int io2 = l.indexOf(o2);
            return io1 - io2;
        }
    };

    public ControladorCaracteristicas getCaracteristicas() {
        return caracteristicas;
    }

    public void setCaracteristicas(ControladorCaracteristicas caracteristicas) {
        this.caracteristicas = caracteristicas;
        cargarCaracteristicas();
    }

    abstract protected void cargarCaracteristicas();

    /**
     * Creates new form AZPanel
     */
    public AZPanel() {
        initComponents();
    }

    /**
     * Carga el combo indicado con los datos de la tabla.
     */
    protected void cargarCombo(String tabla, JComboBox comboBox) {
        String selected = null;
        if (comboBox.getSelectedItem() != null) {
            selected = comboBox.getSelectedItem().toString();
        }
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement("");
        List<String> l = this.caracteristicas.getCaracteristica(tabla);
        for (String m : l) {
            model.addElement(m);
        }
        comboBox.setModel(model);
        if (selected != null) {
            comboBox.setSelectedItem(selected);
        }
    }

    protected void agregarCombosALineasFactura(JTable a, String[] c, ActionListener[] act) {
        JComboBox combo;
        TableColumn col;
        for (int i = 0; i < c.length; i++) {
            if (c[i] != null && !c[i].equals("")) {
                combo = new JComboBox();
                if (act[i] != null) {
                    combo.addActionListener(act[i]);
                }
                cargarCombo(c[i], combo);
                col = a.getColumnModel().getColumn(i);
                col.setCellEditor(new DefaultCellEditor(combo));
            }
        }
        /*
         * Vuelve parametrizable los codigos como el siguiente
         cargarCombo("descripciones", combo);
         col = a.getColumnModel().getColumn(0);
         col.setCellEditor(new DefaultCellEditor(combo));

         combo = new JComboBox();
         cargarCombo("talles", combo);
         col = a.getColumnModel().getColumn(1);
         col.setCellEditor(new DefaultCellEditor(combo));

         combo = new JComboBox();
         cargarCombo("colores", combo);
         col = a.getColumnModel().getColumn(2);
         col.setCellEditor(new DefaultCellEditor(combo));
         */
    }

    /**
     * Carga una tabla simple con los datos de la caracteristica
     */
    protected void cargarTable(String caracteristica, JTable tabla, JTable tabla2) {
        List<String> l_orig = this.caracteristicas.getCaracteristica(caracteristica);
        List<String> l = new ArrayList<String>();
        for (String aux : l_orig) {
            l.add(aux);
        }
        TableModel t2 = tabla2.getModel();
        for (int i = 0; i < t2.getRowCount(); i++) {
            l.remove(t2.getValueAt(i, 0));
        }
        DefaultTableModel model = (DefaultTableModel) tabla.getModel();
        while (model.getRowCount() > 0) {
            model.removeRow(0);
        }
        for (int i = 0; i < l.size(); i++) {
            model.addRow(new Object[]{l.get(i)});
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
